package com.example.segment_saver;
/*
 * Nov 16th 2012
 * Created By: Alex Drotar, Jacob Reiger 
 * Using code from: Ken Anderson 
 * Segment_Saver- This is meant to be run on Andriod mobile devices
 * 					The app will allow the user to take in a url that they
 * 					want to watch. If the user selects the update site button
 * 					The application will go and check if the site has changed 
 * 					since the last time the user looked at it.
 * 
 */
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;

import org.jsoup.*;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.util.Log;

public class SiteData implements Serializable {

	private static final long serialVersionUID = 1L;
	
	private String url;
	private String SavedHTML;
	private String SavedURL;
	private ArrayList<String> URLlist;
	
	public SiteData(){
		try{
			this.url = "www.google.com";
		}
		catch(Exception e)
		{
			
		}
	}
	
	public SiteData(String url)
	{
		try{
			this.url= url;
			Log.i("Segment Saver", "This is the URL " + this.url);
		}
		catch(Exception e)
		{
			Log.i("Segment Saver", "Error in SiteData constructor");
		}

		//Log.i("Segment Saver", "This is the URL " + this.url);


	}
	
	public String getUrl(){
		return this.url;
	}
	
	public void setUrl(String url)
	{
		this.url=url;
	}
	private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException
	{
		this.url = (String) stream.readObject();
		this.SavedHTML = (String) stream.readObject();
		this.URLlist = (ArrayList<String>) stream.readObject();
		//Log.i("Segment Saver", "this is the URL being loaded " +this.url);

	}
	
	private void writeObject(ObjectOutputStream stream) throws IOException
	{
		//Log.i("Segment Saver", "this is the URL being sent to file " +this.url);

		stream.writeObject(this.url);
		stream.writeObject(this.SavedHTML);
		stream.writeObject(this.URLlist);
	}
	
	public void Set_SavedHTML()
	{
		try
		{
			SavedHTML = Jsoup.connect(url).get().html();
		}
		catch(Exception e)
		{
			if(e.getMessage() != null)
			{
				Log.i("SiteData", e.getMessage());
			}
			else
			{
				Log.i("SiteData", "Null exception thrown");
				e.printStackTrace();
			}
		}
	}
	public boolean doSitesDiffer()
	{
		try
		{
			String New = Jsoup.connect(url).get().html();
			
			if(New.equals(SavedHTML))
			{
				
			}
			else
			{
				SavedHTML = New;
				this.AreURLsEqual();
				return true;
			}
			
			if(!this.AreURLsEqual())
			{
				return true;
			}
			else
			{
				return false;
			}
			
			
			
		}
		catch(Exception e)
		{
			//Log.i("Website", "Error in doSitesDiffer");
			return false;
		}
		
	}
	
	public boolean AreURLsEqual()
	{
		try{
			
			ArrayList<String> tempList = new ArrayList<String>();
			
			Document New = Jsoup.connect(url).get();
			
			Elements divs = New.select("*");
			
			for(Element div : divs)
			{
				if(div.hasAttr("src"))
				{
					tempList.add(div.attr("src"));
				}
			}
			
			if(tempList.equals(URLlist))
			{
				Log.i("Segment Saver", "URLlist are equal");
				return true;
			}
			else
			{
				URLlist = tempList;
				Log.i("Segment Saver", "URLlist aren't equal");
				return false;
			}
		}
		catch(Exception e){
			return false;
		}
	}
	public boolean has_saved_url()
	{
		try
		{
			Document New = Jsoup.connect(url).get();
			
			Elements divs = New.select("*");
			
			for(Element div : divs)
			{
				if(div.hasAttr("src"))
				{
					System.out.println(div.attr("src"));
					if(div.attr("src").equals(SavedURL))
					{
						return true;
					}
				}
			}
			return false;
		}
		catch(Exception e)
		{
			Log.i("Profile Viewer", "Profiles saved to ");
			return false;
		}
		
		
	}
}
